package com.njwb.Swing.Supplier;

import com.njwb.pojo.Supplier;
import com.njwb.Util.UtilService;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.function.Consumer;

public class SupplierDialog extends JDialog {
    private JTextField nameField;
    private JTextField descriptionField;
    private JTextField contactField;
    private JTextField phoneField;
    private JTextField addressField;
    private JButton confirmButton;
    private JButton cancelButton;

    private UtilService utilService;
    private Supplier supplier; // 如果不为 null 表示修改
    private Consumer<Boolean> callback; // 操作成功后回调

    public SupplierDialog(Frame owner, String title, Supplier supplier, Consumer<Boolean> callback) {
        super(owner, title, true);
        this.supplier = supplier;
        this.callback = callback;
        this.utilService = new UtilService();

        setSize(400, 300);
        setLocationRelativeTo(owner);
        setLayout(new GridLayout(6, 2, 5, 5));

        add(new JLabel("供应商名称:"));
        nameField = new JTextField();
        add(nameField);

        add(new JLabel("供应商品:"));
        descriptionField = new JTextField();
        add(descriptionField);

        add(new JLabel("联系人:"));
        contactField = new JTextField();
        add(contactField);

        add(new JLabel("电话:"));
        phoneField = new JTextField();
        add(phoneField);

        add(new JLabel("地址:"));
        addressField = new JTextField();
        add(addressField);

        confirmButton = new JButton("确认");
        cancelButton = new JButton("取消");
        add(confirmButton);
        add(cancelButton);

        // 如果是修改，将原数据填充
        if (supplier != null) {
            nameField.setText(supplier.getName());
            descriptionField.setText(supplier.getDescription());
            contactField.setText(supplier.getContact_person());
            phoneField.setText(supplier.getPhone());
            addressField.setText(supplier.getAddress());
        }

        // 按钮事件
        confirmButton.addActionListener(e -> {
            String name = nameField.getText().trim();
            String description = descriptionField.getText().trim();
            String contact = contactField.getText().trim();
            String phone = phoneField.getText().trim();
            String address = addressField.getText().trim();

            if (name.isEmpty()) {
                JOptionPane.showMessageDialog(this, "供应商名称不能为空！");
                return;
            }

            boolean success = false;
            if (supplier == null) {
                // 新增
                success = utilService.addSupplier(name, description, contact, phone, address);
            } else {
                // 修改
                success = utilService.updateSupplier(
                        supplier.getSupplier_id(),
                        name, description, contact, phone, address
                );
            }

            if (success) {
                JOptionPane.showMessageDialog(this, supplier == null ? "新增成功！" : "修改成功！");
                dispose();
                if (callback != null) callback.accept(true);
            } else {
                JOptionPane.showMessageDialog(this, supplier == null ? "新增失败！" : "修改失败！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        cancelButton.addActionListener(e -> dispose());
    }
}
